package lan.app;

import jade.content.onto.basic.Action;
import jade.wrapper.gateway.JadeGateway;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.swing.plaf.basic.BasicSliderUI.ActionScroller;

import lan.bean.BlackBoardBean;


/**
 * Servlet implementation class GateWayServlet
 */
public class GateWayServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public GateWayServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see Servlet#init(ServletConfig)
	 */


    @Override
	public void init() throws ServletException {
		// TODO Auto-generated method stub
		super.init();
		JadeGateway.init("lan.agents.MyGateWayAgent", null);
	}
		
	

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		String actionName = request.getParameter("action");
	
		if(actionName == null){
			response.sendError(HttpServletResponse.SC_NOT_ACCEPTABLE);
		return;
		}
		
		BlackBoardBean board = new BlackBoardBean();
		board.setReceiver("ping");
		board.setMessage("hey,what are u doing");
		try{
			JadeGateway.execute(board);
			
		}catch(Exception e){
			e.printStackTrace();
			
		}
		
		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		out.print("Message has been sent!<br/>");
		out.print("Reply :"+ board.getMessage());
		
		out.print("<br/> <a href ='index.html'>Go back</a>");
		response.flushBuffer();
	}
}
